<!-- 申请退款 -->
<template>
  <div>
    <van-nav-bar
      fixed placeholder
      title="订单确认"
      left-arrow
      @click-left="$router.back()"
    />

    <div class="orderbox">
      <div class="address">
      
          <div><van-icon name="location" size="28"/></div>
          <div v-if="haveAddress">
            <div>
              <span class="name">{{address.addressName}}</span>
              <span class="light">{{address.addressPhone}}</span>
            </div>
            <div class="light">{{address.addressContent}}</div>
          </div>
          <div v-else>请选择地址</div>
          <div><van-icon name="arrow" size="26" color="#DDDDDD" @click="selectAddress"/></div>
        
      </div>
      <div class="orderGood">
        <div>订单商品</div>
        <div>
          <van-card
            :price="playerBuy.goodsFormatPrice"
            :desc="goodsInfo.goodsDesc"
            :title="goodsInfo.goodsName"
            :thumb="'/tu/'+goodsInfo.goodsImg"
          >
           <template #tags>
            <van-col >{{goodsFormat.goodsFormatName}}</van-col>
          </template>
          </van-card>
        </div>
        <div class="buyNum">
          <div>购买数量</div>
          <div class="buyBtn">
            <van-button icon="minus" size="mini" @click="num--" :disabled="num==1"/>
            <van-field v-model="num" type="digit" style="width:40px"/>
            <van-button icon="plus" size="mini" @click="num++" :disabled="num==goodsInfo.goodsLimitBuy || orderType==2" />
          </div>
        </div>
      </div>
     <div>
       <!-- 是否能够使用优惠券 -->
      <div v-if="goodsInfo.goodsCoupon==1 && (orderType==1 || orderType==0)">
        <van-coupon-cell
          :coupons="coupons"
          :chosen-coupon="chosenCoupon"
          @click="showList = true"
        />
        <van-popup
         v-model="showList"
          round
          position="bottom"
          style="height: 90%; padding-top: 4px;"
        >
          <van-coupon-list
            :coupons="coupons"
            :chosen-coupon="chosenCoupon"
            :disabled-coupons="disabledCoupons"
            @change="onChange"
            empty-image='https://img01.yzcdn.cn/vant/coupon-empty.png'
             :show-exchange-bar='false'
          />
        </van-popup>
      </div>
      <div>
        <van-cell-group>
          <van-cell title="运费" :value="'￥'+ShippingMoney" border/>
          <van-cell title="合计" :value="'￥'+((playerBuy.goodsFormatPrice*num)-couponMoney+ShippingMoney)" border />
        </van-cell-group>
      </div>
     </div>
     
    
    
      <!-- <div >
        <van-radio-group v-model="checked" >
          <van-cell title="微信支付" clickable >
            <template #right-icon>
              <van-radio name="1" />
            </template>
          </van-cell>
        </van-radio-group>
      </div> -->
    </div>

    
    <van-submit-bar :price="((playerBuy.goodsFormatPrice*num)-couponMoney+ShippingMoney)*100" button-text="提交订单" @submit="onSubmit"/>
  </div>
</template>

<script>
export default {
  data () {
    return {
      haveAddress:false,
      num:1,
      chosenCoupon: -1,
    
      showList:false,
      checked:"1",
      coupons: [],
      disabledCoupons: [],
      address:{
        // addressId:1,
        // addressName:'',
        // addressPhone:'',
        // addressContent:''
      },
      userId:1,
      goodsInfo:{},
      goodsFormat:{},
      couponId:0,
      totalMoney:0,
      couponMoney:0,
      ShippingMoney:0,
      playerBuy:{
        goodsId: 2,
        selectedNum: 1,
        goodsFormatId: 84,
        goodsFormatPrice: 699
      },
      orderType:0,
      groupId:0,
      flashId:0,
      helpTeamId:0,//助力成功
    }
    
  },

  created(){
    this.userId=JSON.parse(sessionStorage.getItem('user')).userId
    
    this.orderType=this.$route.query.buyState
    if(this.orderType==1){
      this.playerBuy=this.$store.state.buyFormat
      this.num=this.playerBuy.selectedNum
    }else if(this.orderType==0){
      this.playerBuy=this.$store.state.buyFormat
      this.groupId=this.$route.query.groupId==undefined?0:this.$route.query.groupId
      this.num=this.playerBuy.selectedNum
    }else if(this.orderType==2){
      this.helpTeamId=this.$router.query.helpTeamId==undefined?0:this.$route.query.helpTeamId
       this.playerBuy.goodsId=this.$route.query.goodsId
      this.playerBuy.selectedNum=1
      this.playerBuy.goodsFormatId=this.$route.query.goodsFormatId
      this.playerBuy.goodsFormatPrice=0
    }else if(this.orderType==3){
      this.flashId=this.$router.query.flashId==undefined?0:this.$route.query.flashId
      this.playerBuy.goodsId=this.$route.query.goodsId
      this.playerBuy.selectedNum=1
      this.playerBuy.goodsFormatId=this.$route.query.goodsFormatId
      this.playerBuy.goodsFormatPrice=this.$route.query.flashPrice
    }
    
    
    this.isHaveAddress()
    this.getCoupon()
    this.getGoodsDetail()
    
  },
  
   methods: {
     getGoodsDetail(){
       this.$axios.post('/api/marketGoods/goodsDetail?goodsId='+this.playerBuy.goodsId).then(res=>{
         console.log(res)
         if(res.data.code==200){
           this.goodsInfo=res.data.data.goodsInfo
           var formatList=res.data.data.goodsInfo.goodsFormatList
           var list=formatList.filter(v=>{
             return v.goodsFormatId==this.playerBuy.goodsFormatId
           })
          this.goodsFormat=list[0]
          this.goodsInfo.goodsFreeShipping==1?this.ShippingMoney=0:this.ShippingMoney=this.goodsInfo.goodsPostage
         }
          // console.log('规格')
          //  console.log(this.goodsFormat)
       })
     },
    getCoupon(){
      //获取优惠券
          this.$axios.post('/api/agoCoupon/userAll?userId='+this.userId).then(res=>{
            // console.log(res)
            if(res.data.code==200){
              console.log(res.data.data)
              // //判断是否达到使用金额
              var canuse=[]
              var notuse =[]
              res.data.data.forEach(v => {
                // console.log("--1111----")
                // console.log(this.playerBuy.goodsFormatPrice*this.num)
                if(v.couponShow.couponShowMinimoney<=this.playerBuy.goodsFormatPrice*this.num
                && v.couponUserType==0 && new Date(v.couponShow.couponEndTime)>=new Date()&& new Date()>=new Date(v.couponShow.couponStartTime)){
                  canuse.push(v)
                }else{
                  notuse.push(v)
                }
              });
              
              canuse.forEach((v)=>{
                this.coupons.push({
                  available: 1,
                  id:v.couponId,
                  condition: v.couponShow.couponShowName+'\n 满'+v.couponShow.couponShowMinimoney+'元可用',
                  value: v.couponShow.couponShowMoney*100,
                  name:  v.couponShow.couponShowName,
                  startAt:Number(new Date(v.couponShow.couponStartTime))/1000,
                  endAt: Number(new Date(v.couponShow.couponEndTime))/1000,
                  valueDesc: v.couponShow.couponShowMoney,
                  unitDesc: '元',
                })
              })
              notuse.forEach((v)=>{
                this.disabledCoupons.push({
                  available: 1,
                  id:v.couponId,
                  condition: v.couponShow.couponShowName,
                  value: v.couponShow.couponShowMoney*100,
                  name:  v.couponShow.couponShowName,
                  startAt:Number(new Date(v.couponShow.couponStartTime))/1000,
                  endAt: Number(new Date(v.couponShow.couponEndTime))/1000,
                  valueDesc: v.couponShow.couponShowMoney,
                  unitDesc: '元',
                })
              })
            
            }
        })
      },
     isHaveAddress(){
       //判断用户是否有地址 默认地址
        this.$axios({
          url:'/api/address/list',
          method:'post',
          params:{userId:this.userId}
        }).then(res=>{
          console.log(res)
          if(res.data.code==200){
            var addressList=res.data.data
            if(addressList.length>0){
              //判断是否有默认地址
              var defaultAddress=addressList.filter(v=>{
                return v.isDefault==1
              })
              // console.log(defaultAddress)
              this.address=defaultAddress[0]
              this.haveAddress=true
            }
          }
        })
     },
     selectAddress(){
       this.$router.push('/addressPage')
     },
    onChange(index) {
      this.showList = false;
      console.log(index)
      if(index==-1){
         this.chosenCoupon = index;
        this.couponMoney=0
      }else{
         this.chosenCoupon = index;
        // console.log(this.coupons[this.chosenCoupon].value)//单位分
        this.couponMoney=this.coupons[this.chosenCoupon].value/100
        this.couponId=this.coupons[this.chosenCoupon].id
        console.log(this.couponId)
      }
     
    },
    onSubmit(){
      if(this.haveAddress){
        this.totalMoney=(this.goodsFormat.goodsFormatPrice*this.num)-this.couponMoney+this.ShippingMoney
        console.log(this.totalMoney)
        //判断是单独购买还是拼团还是其他活动
        //单独购买
        var onlyBuy={}
        if(this.couponId==0){
          onlyBuy={
              goodsFormatId:this.playerBuy.goodsFormatId,
              addressId:this.address.addressId,
              userId:this.userId,
              orderType:1,
              orderGoodsNum:Number(this.num)
            }
        }else{
          onlyBuy={
              goodsFormatId:this.playerBuy.goodsFormatId,
              addressId:this.address.addressId,
              couponId:this.couponId,
              userId:this.userId,
              orderType:1,
              orderGoodsNum:Number(this.num)
            }
        }
        // console.log(onlyBuy)
        // console.log(onlyBuy)
        //发起团购
        
        var pinBuy={}
        if(this.couponId==0){
            pinBuy={
              goodsFormatId:this.playerBuy.goodsFormatId,
              addressId:this.address.addressId,
              userId:this.userId,
              orderType:0,
              orderGoodsNum:Number(this.num),
              defineGroupPrice:this.playerBuy.goodsFormatPrice
            }
        }else{
          pinBuy={
              goodsFormatId:this.playerBuy.goodsFormatId,
              addressId:this.address.addressId,
              couponId:this.couponId,
              userId:this.userId,
              orderType:0,
              orderGoodsNum:Number(this.num),
              defineGroupPrice:this.playerBuy.goodsFormatPrice
            }
        }
      
        //团购
        var pinGroupBuy={}
        if(this.couponId==0){
            pinGroupBuy={
              goodsFormatId:this.playerBuy.goodsFormatId,
              addressId:this.address.addressId,
              userId:this.userId,
              orderType:0,
              orderGoodsNum:Number(this.num),
              groupId:this.groupId,
              defineGroupPrice:this.playerBuy.goodsFormatPrice
            }
          
        }else{
          pinGroupBuy={
              goodsFormatId:this.playerBuy.goodsFormatId,
              addressId:this.address.addressId,
              couponId:this.couponId,
              userId:this.userId,
              orderType:0,
              orderGoodsNum:Number(this.num),
              groupId:this.groupId,
              defineGroupPrice:this.playerBuy.goodsFormatPrice
            }
        }
        //秒杀
        if(this.flashId !=0){
            var miaosha={
            goodsFormatId:this.playerBuy.goodsFormatId,
            addressId:this.address.addressId,
            userId:this.userId,
            orderType:3,
            orderGoodsNum:Number(this.num),
            flashId:this.flashId,
          }
        }
        
        //助力订单
        if(this.helpTeamId !=0){
          var helpOrder={
            goodsFormatId:this.playerBuy.goodsFormatId,
            addressId:this.address.addressId,
            userId:this.userId,
            orderType:2,
            helpTeamId:this.helpTeamId,
            orderGoodsNum:Number(this.num),
          }
        }
      
        //提交订单，生成订单号
        //1 单独购买
          console.log(this.orderType)
          if(this.orderType==1){
          
            this.$axios({
              method:'post',
              url:'/api/marketOrder/add',
              params:onlyBuy
            }).then(res=>{
              console.log(res)
              if(res.data.code==200){
                var orderId=res.data.data
                this.$router.push('/suceess?orderId='+orderId)
              }
            })
                
          }else if(this.orderType==0 && this.groupId==0){ //发起拼团
              console.log(pinBuy)
                this.$axios({
                  method:'post',
                  url:'/api/marketOrder/add',
                  params:pinBuy
                }).then(res=>{
                  console.log(res)
                  console.log(res.data.data)
                  if(res.data.code==200){
                    var orderId=res.data.data
                    this.$router.push('/suceess?orderId='+orderId)
                  }
                })
          }else if(this.orderType==0 && this.groupId!=0){ //参与拼团
              console.log(pinGroupBuy)
              this.$axios({
                method:'post',
                url:'/api/marketOrder/add',
                params:pinGroupBuy
              }).then(res=>{
                console.log(res)
                console.log(res.data.data)
                if(res.data.code==200){
                  var orderId=res.data.data
                  this.$router.push('/suceess?orderId='+orderId)
                }
              })
          }else if(this.orderType==3){ //秒杀
              console.log(miaosha)
              this.$axios({
                method:'post',
                url:'/api/marketOrder/add',
                params:miaosha
              }).then(res=>{
                console.log(res)
                console.log(res.data.data)
                if(res.data.code==200){
                  var orderId=res.data.data
                  this.$router.push('/suceess?orderId='+orderId)
                }
              })
          }else if(this.orderType==2){ //助力成功
          console.log(helpOrder)
            this.$axios({
              method:'post',
              url:'/api/marketOrder/add',
              params:helpOrder
            }).then(res=>{
              console.log(res)
              if(res.data.code==200){
                var orderId=res.data.data
                this.$router.push('/suceess?orderId='+orderId)
              }
            })
          }
      }else{
        this.$toast.fail('请选择收货地址')
      }
      

    
    },
   
  }
}
</script>

<style  scoped>
  .orderbox{
    background-color: #F9F9F9;
  }
  .orderbox>div{
    background-color: white;
    margin-bottom: 20px;
  }
  .address .van-icon{
    color: #FF6633;
  }
  .address{
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 20px 0;
  }
  .light{
    font-size: 14px;
  }
  .name{
    font-weight: 500;
    padding-right: 10px;
  }
  .orderGood{
    padding: 10px;
    text-align: left;
  }
  .van-card{
    text-align: left; 
    margin: 10px 0;
  }
  .buyNum{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .buyBtn{
    display: flex;
    align-items: center;
  }
  .van-cell__title,
  .van-submit-bar__text{
    text-align: left;
  }
</style>
